#!/usr/bin/perl -w
# Author: Mina Ramses
# Date: 30.11.05
# Purpose: coding Reference

# Read a file and parse the contents whatever it's as a multidimension array elements 
	open(HAN1, "list") || die "Error opening List: $!";  #File open or die on error
	while(<HAN1>){
	  push @array1, [split];	 #split file contents into the array1
	}
	
	foreach(@array1){
	print "@$_\n"; 			#print the array contents whatever it's 
	}

	print "$array1[0][1]\n"      #print the first element of a multidimensioanl (2 dimension) array

	#---------------------------------------------------------
	# Array slicing 

	use strict ;
	my @array_list = qw(First Second Third Fourth Fifth sixth);
	print "@array_list\n";
	print "@array_list[0..2]\n";
	print "@array_list[0..$#array_list]\n";
	print "@array_list[0..3,5]\n";
        
	#---------------------------------------------------------
	#Array Functions (POP, PUSH,(Last element) Shift, Unshift(First element) and Sort)
	
	use strict;
	my @array2=("1","2","3");
	my $ppop = pop @array2;
	print "$ppop\n";
	my $name ="mina";
	push @array2 , $ppop;
	print "@array2\n";
	push @array2, $name;
	print "@array2\n";
	unshift (@array2,"$name");
	print "@array2\n";
	shift @array2;
	print "@array2\n";
	my @array2 = sort @array2;
	print "After Sorting : ", "@array2\n";

	#--------------------------------------------------------
	#Conditionals
	
	if (2<3){# < > <= => == , compared values can be 
	         # either Scalars apply to (eq,), lists , hashes variables
	 print "2 is less than 3";
	}
	else {
	 print "Error\n";
	 }
	unless (2==3){ # Conditional negation
	 print "Arent'equal\n";
	 }
	 
	#--------------------------------------------------------
	#Positional Parameters /Extract command line Arguments 
	
	print "#$ARGV[0]\n";
	print "$#ARGV\n"; # print Arguments number
       # Controling the script to accept 3 arguments 
        $REQ_ARG = 3;
	$BADARGS = 165;
	$#ARGV += 1; #increment the list of ARGV to start by 0
	unless ($ARGV == $REQ_ARG){
	 print"$0 requires $REQ_ARG Arguments\n";
	 exit $BADARGS;  #for help on exit() function use perl -f exit
	}
	print "@ARGV[0..$#ARGV]\n";
	
	#--------------------------------------------------------
	#For Loops
	
        for ($i=1;$i<=10;$i++){
	print "$i\n";
	}
	
	$min = 1;
	$max = 10;
	for ($min..$max){print "this's the out\n";}
	
	@array3=("Mina","Ramses","Fahmy","Debian","Tux","forked()");
	foreach(@array3){
	 print"$_ "; #print the array full contents
	 }
	
	#--------------------------------------------------------
	#While loops
	
	$value1 = 1;
	$valeu2 = 2;
	while ($value1<=$value2){
	  print "$value1\n";
	  $value1+=1;
	}
        

	until ($value2<=$value1){
	 print "$value2\n";
	 $value2 -= 1;
	}
	
	#---------------------------------------------------------
	#File I/O     write > , append >>,  read 
	 
        $min =1; 
	$max =10;
	$fullname = "Mina ramses";
	open (OUTFILE, ">list2") || die "Problems: $!";
	#open (FILE, "+<list2") || die "problem: $!"; /To read and write to file list2
	#open (FILE, "ps -ax |") || die "problem: $!"; /To use a process as a file
	#if(/httpd$/){print "$_";} /grep for httpd from the ps -ax |
	#
	for ($min..$max);{print OUTFILE "$fullname\n";}

	
	open (INFILE, "list2")   || die "Problems: $!";
	while(<INFILE>){
	 print "$_ \n";
	}
         
        @list2_contents = <INFILE>; #parse list2 contents into the array
	foreach(@list2_contents){print "$_ \n";}
        
	foreach(@list2_contents){print OUTFILE "$_ \n";} # to copy file _in_ to _out_

	foreach(@list2_contents){
	 if(/Mina/){          #Searh for mina and print if found
	 print OUTFILE "$_ \n";
	  }
	 }

       	foreach()@list2_contents){
        s/Mina/Debian_TUX/;       # replace the Mina Value with Debian_Tux
	print OUTFILE "$_ \n";
	}
	
	$DIR = "/home/mina/coding/Perl";
	$DIRHANDLE ="HANDLE";
	opendir ($DIRHANDLE, "$DIR") || die "Error opening $DIR: $!";
	@dirlist = readdir($DIRHANDLE);
	foreach (@dirlist){
		print "$_\n";
	}

	#@dirlist = 'ls -A $DIR';
	#	foreach(@dirlist){
	#		print"$_ ";
	#	}
	

        $data1 ="Mina Ramses";
	@array4 = split /\s+/, $data1; #split function concerned with spaces s+ is the space delimeter
	foreach(@array4){
		print"$_\n";
	}	
		
	
	$lc = 0;	#line counter 
		while(<>){ 
		print "$_"; #print the actual contents of the file using $_
		@array5 = split /:/, $_;  #split the contents and stores it in array5 we also use \s+ or :
		#foreach(@array5){      #nested loop 
		#         print"$_\n"        
		#         }
		$lc += 1;
		}
	print "Line count is equal to: ", "\t", "$lc","\n";


        
	
	my @array6 = ("Mina","Ramses","Fahmy");
	my $newarray = join ":",array6;
	 print "This is the output \":\"","$newarray", "\n"; # the delimeter is ":" 


	 
	while(<>){
	 @array7 = split /:/, $_;           # splitted with the delimeter : 
	 $newarray2 = join ",", @array1;    # and joined with the , delimeter
	 print "New output delimeted by \",\":", "$newarray2\n";
	 }


         my $data1 = "dataproducts;"
	 open (han1,"$data1")|| die "erros opening file $data1: $!";
	 my @f1 = <han1>;
	 my $data = 0;
	 foreach(@f1){
	 print "$_";
	 }
	 foreach(@f1){
         print "$_";
	 my @columns= split;            #split to get a specified output
	 print "$columns[4]", "\n";     # get the fourth line of the splitted  data.
         }
	 
	 

	 open(HAN, "teststrings.txt") || die "errors opening file: $!";
	 @f2=<HAN>;
	 foreach(@f2){
	 print "$_\n";
	 $new = uc;   #change to upper case the contents of @f2 also lc for lower case
	 print "$new";
	 }

	 $DIR = "/root/temp1/perl/file*";
	 @filelist = 'ls -A $DIR';
         foreach(@filelist){
	 print "$_";
	 chomp;
	 $new = uc;
	 print "$new";
	 rename($_,$new);
	 }
	 #-------------------------------------------------------------------
	 #
	 #Regular Expressions

	 $firstname= "mina";
	  if($firstname =~ /mina/){  #searchs for the given value =~
	                #=~ /^mIna/i)  seach for the value at the beginning using /^ and ingnoring case i
                        #=- /mINa$/i)  /$ searsh at the end
               print "$firstname\n";	  
	  }
         
	 
	 @array7=("Mina ramses","First Mina");

	 foreach(@array7){
	 s/Mina/Eng.M/; #substituation for mina with Eng.M
	 #s/Mina/Eng.M/g;
	 #s/Mina\s/Eng.M; removes the spaces and substitute
	 print"$_\n";
	 }

	 $var3 = "mina ramses 1 2 3 4 ";
	 # \d = extracts only digits anything but digit use \D
	 # \s = spaces
	 # \w = word
	 $var1=~/(\s\d)(\s\d)(\s\d)/;# $1,$2, $3, etc.


	 $time='date %r';
	 $time=~/(\d\d):(\d\d):(\d\d)\s(.*)/; #extracts the time components of the date cmd.
	 print"$1 $2 $3 $4\n";
	 
         
	 @array8 = ("might","night","right","tight","goodnight","test");
	 foreach(@array8){
	 if (/[mnrt]ight/){       # match the common characters
	 #if (/dog|car|bird/i){   matching with given values
	   print "$_\n";
	   }
         }

         $IN = "INFILE";
	 $filename = "";
	 open(IN,"$filename")|| die "$!";
         while(<$in>){
	 if(/\/etc/){
	 #if (/\\/)for windows paths values
	 #if(/(.:)(.*\\)/)
	 #print "$1""$2";   _ C: _  ,  _ \documents and settings\ _
	 #if(/\+/){  mathematics expresions
	 #if (/yes/i) ignoring the case 
	 #if (/..y/) parse strings like cry , try...
	 #if (/.y/) parse strings like my 
	 #if (/[a-z]\d+/i) alphanumeric strings 
	 #if (/.*@.*/) regular exp for emails
	 #if (/^$/) blank lines
	 print "$_";
	  }
	 }
        #-----------------------------------------------------------------------------
	#
	#Common functions  shell interations
	
	$DIR = "/etc/init.d";
	system "$DIR/httpd stop";
	print "$?\n"; #output _$?_

	#also works/.
	#@array9=("$DIR/httpd","stop");
	#system(@array9);
        
	@filelist2 = 'ls -Al $DIR';
	#$filelist 'ls -Al $DIR';
	 foreach(@filelist){
	  print "$_";
	 }
	 #-----------------------------------------------------------------------------
	 #
	 #Mail integration.
